﻿using GrainManage.Entities.Image;
using GrainManage.Proxy;
using GrainManage.Web.Common;
using GrainManage.Web.Models.Image;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GrainManage.Web.Controllers
{
    public class ImageController : BaseController
    {
        public ActionResult ValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(4);
            Session[Constant.ValidateCode] = code;
            byte[] bytes = vCode.CreateValidateGraphic2(code);
            return File(bytes, @"image/jpeg");
        }

        [HttpPost]
        public ActionResult GetImageUrl(int[] imageID)
        {
            var inputPara = new InputGetImageUrl
            {
                Authentication = CurrentAccount,
                ImageIDs = new List<int>(imageID)
            };
            var imageProxy = ObjectFactory.ImageProxy;
            var result = imageProxy.GetImageUrl(inputPara);
            return NewtonsoftJson(result);
        }

        //[HttpPost]
        public ActionResult Download(int imageID)
        {
            //return Redirect(AppConfig.GetValue("ApiServer") + "Image");
            return Content(Url.Action("Download"));
        }

        [HttpPost]
        public ActionResult Insert(AddImageModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }
            var inputPara = new InputInsert
            {
                Authentication = CurrentAccount,
                Image = new Data.Model.Table.ImageTable
                {
                    ImageName = input.ImageName,
                    Remarks = input.Remarks
                }
            };
            var imageProxy = ObjectFactory.ImageProxy;
            var result = imageProxy.Insert(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return View(Constant.RestStatusView, result);
        }

        [HttpPost]
        public ActionResult Update(UpdateImageModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }
            var inputPara = new InputUpdate
            {
                Authentication = CurrentAccount,
                Image = new Data.Model.Table.ImageTable
                {
                    ImageID = input.ImageID,
                    ImageName = input.ImageName,
                    Remarks = input.Remarks
                }
            };
            var imageProxy = ObjectFactory.ImageProxy;
            var result = imageProxy.Update(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return View(Constant.RestStatusView, result);
        }

        [HttpPost]
        public ActionResult Delete(int imageID)
        {
            var inputPara = new InputDelete
            {
                Authentication = CurrentAccount,
                ImageID = imageID
            };
            var imageProxy = ObjectFactory.ImageProxy;
            var result = imageProxy.Delete(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return View(Constant.RestStatusView, result);
        }

    }
}
